package net.whzjkj.gateway.tcpserver.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: Mr.Hu
 * @create: 2018-11-06 11:56
 **/

@Configuration
@EnableConfigurationProperties(RabbitProperties.class)
public class RabbitConfig {
    @Autowired
    RabbitProperties properties;
    public static final String MESSAGE_DATA_QUEUENAME = "terminal_gateway_message_data";

    @Value("${spring.rabbitmq.message_data.channelCacheSize}")
    private int messageDataChannelCacheSize;
    @Value("${spring.rabbitmq.message_data.prefetchCount}")
    private int messageDataPrefetchCount;
    @Value("${spring.rabbitmq.message_data.exchange}")
    private String messageDataExchange;
    @Value("${spring.rabbitmq.message_data.pattern}")
    private String messageDataPattern;



    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(properties.getHost());
        connectionFactory.setUsername(properties.getUsername());
        connectionFactory.setPassword(properties.getPassword());
        connectionFactory.setPort(properties.getPort());
        connectionFactory.setVirtualHost(properties.getVirtualHost());
        connectionFactory.setChannelCacheSize(messageDataChannelCacheSize);
        return connectionFactory;
    }
    /**
     * 消息转换器
     * @return
     */
    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    /**
     * 消息数据队列消息代理
     * @param factory
     * @return
     */
    @Bean
    public AmqpAdmin messageDataAdmin(@Qualifier("connectionFactory") ConnectionFactory factory) {
        return new RabbitAdmin(factory);
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setMessageConverter(jackson2JsonMessageConverter());
        return template;
    }

    /**
     * 消息数据队列交换机
     * @return
     */
    @Bean
    public TopicExchange messageDataExchange() {
        return new TopicExchange(messageDataExchange, true, false);
    }

    /**
     * 消息数据队列
     * @return
     */
    @Bean
    public Queue messageDataQueue() {
        return new Queue(MESSAGE_DATA_QUEUENAME, true, false, false);
    }

    /**
     * 消息数据队列绑定
     * @return
     */
    @Bean
    public Binding messageDataBinding() {
        return BindingBuilder.bind(messageDataQueue()).to(messageDataExchange()).with(messageDataPattern);
    }
}

